import BunnyCDNStorage
import os


def upload_file(file_path, cdn_path, file_name):
    AccessKey = "db805d54-70e4-458c-bdc06ba6ddb4-f1eb-4f64"
    cdn_connector = BunnyCDNStorage.CDNConnector(AccessKey, 'web-apkpoki')
    res = cdn_connector.upload_file(cdn_path, file_name, file_path)
    if res['HttpCode'] == 201:
        return True
    else:
        print(res)
        return False


def upload(path, num, addr='game', filename=''):
    print(11)
    if os.access(path, os.F_OK):
        print(22)
        try:
            print(33)
            res = upload_file(path, addr, filename)
            print(44)
            if res == True:
                with open('file.txt', 'a') as w:
                    w.write('第' + str(num) + '个文件->' + filename + '<-已上传完成-------->' + filename + '\n')
                    w.close()
                print('第' + str(num) + '个文件->' + filename + '<-已上传完成')
                os.remove(path)
                return True
            else:
                with open('file_error.txt', 'a') as w:
                    w.write('第' + str(num) + '个文件->' + filename + '<-上传失败-------->' + filename + '\n')
                    w.close()
                print('第' + str(num) + '个文件->' + filename + '<-上传失败-------->' + filename)
                os.remove(path)
                return False
        except Exception:
            with open('file_error.txt', 'a') as w:
                w.write('第' + str(num) + '个文件->' + filename + '<-上传失败-------->' + filename + '\n')
                w.close()
            print('第' + str(num) + '个文件->' + filename + '<-上传失败-------->' + filename)
            os.remove(path)
            return False
        os.remove(path)
